import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:ugrownews_flutter/main/ScreenSearchResult.dart';
import 'package:ugrownews_flutter/utils/TextUtil.dart';
import 'package:ugrownews_flutter/utils/Toast.dart';
import 'package:ugrownews_flutter/utils/UGrowColors.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/cupertino.dart';

class ScreenSearch extends StatefulWidget {
  @override
  _ScreenSearchState createState() => _ScreenSearchState();
}

class _ScreenSearchState extends State<ScreenSearch> {
  List<String> searchHistory = new List();

  @override
  void initState() {
    super.initState();
    _getHistory().then((searchHistory) {
      if (searchHistory != null && searchHistory.length > 0) {
        this.searchHistory = searchHistory;
      }
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    TextEditingController controller = new TextEditingController();
    return new Scaffold(
      //整个屏幕
      body: new Container(
        alignment: Alignment.topCenter,
        color: Colors.white,
        padding: new EdgeInsets.only(top: MediaQuery.of(context).padding.top),
        height:
            ScreenUtil.screenHeightDp - MediaQuery.of(context).padding.bottom,
        width: ScreenUtil.screenWidthDp,
        //去掉通知栏的部分
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            //顶部搜索条
            new Container(
              height: ScreenUtil.getInstance().setWidth(100),
              width: ScreenUtil.screenWidthDp,
              child: new Stack(
                children: <Widget>[
                  new Container(
                    height: ScreenUtil.getInstance().setWidth(100),
                    width: ScreenUtil.getInstance().setWidth(64),
                    alignment: Alignment.centerLeft,
                    child: new Image.asset(
                      "images/search_topimage.png",
                      height: ScreenUtil.getInstance().setWidth(64),
                      width: ScreenUtil.getInstance().setWidth(64),
                      fit: BoxFit.fill,
                    ),
                    margin: new EdgeInsets.only(
                        left: ScreenUtil.getInstance().setWidth(36)),
                  ),
                  new Container(
                    margin: new EdgeInsets.only(
                        left: ScreenUtil.getInstance().setWidth(140)),
                    height: ScreenUtil.getInstance().setWidth(100),
                    width: ScreenUtil.screenWidthDp -
                        ScreenUtil.getInstance().setWidth(127 + 45 + 100 + 45),
                    alignment: Alignment.centerLeft,
                    child: new TextField(
                      textInputAction: TextInputAction.search,
                      onSubmitted: (str) {
                        //点了搜索按钮之后的操作

                        if (!TextUtil.isEmpty(str)) {
                          goSearchList(str);
                          searchHistory.remove(str);
                          searchHistory.insert(0, str);
                          _changeHistory(searchHistory);
                          controller.clear();
                        }
                      },
                      controller: controller,
                      keyboardType: TextInputType.text,
                      decoration: new InputDecoration(
                        border: InputBorder.none,
                        hintText: "搜索新闻",
                        contentPadding: new EdgeInsets.symmetric(vertical: 0),
                      ),
                    ),
                  ),
                  new Positioned(
                    right: ScreenUtil.getInstance().setWidth(45),
                    height: ScreenUtil.getInstance().setWidth(100),
                    width: ScreenUtil.getInstance().setWidth(100),
                    child: new Container(
                      alignment: Alignment.centerLeft,
                      child: new GestureDetector(
                        child: new Text("取消"),
                        onTap: () {
                          Navigator.of(context).pop();
                        },
                      ),
                    ),
                  ),
                ],
              ),
            ),
            //搜索条与列表分割线
            new Container(
              height: ScreenUtil.getInstance().setWidth(2),
              width: ScreenUtil.screenWidthDp,
              color: UGrowColors.getColor("#eeeeee"),
            ),
            //搜索历史列表
            new Expanded(
              child: new Container(
                padding: new EdgeInsets.only(
                    left: ScreenUtil.getInstance().setWidth(36),
                    right: ScreenUtil.getInstance().setWidth(36)),
                child: new ListView.builder(
                  controller: new ScrollController(),
                  padding: EdgeInsets.all(0),
                  itemCount: searchHistory.length,
                  itemBuilder: _buildItem,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildItem(BuildContext context, int index) {
    return new Container(
      height: ScreenUtil.getInstance().setWidth(124),
      width: ScreenUtil.screenWidthDp,
      child: new Column(
        children: <Widget>[
          new Container(
            height: ScreenUtil.getInstance().setWidth(122),
            child: new Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                new Image.asset(
                  "images/search_history.png",
                  width: ScreenUtil.getInstance().setWidth(40),
                  height: ScreenUtil.getInstance().setWidth(40),
                ),
                new GestureDetector(
                  onTap: () {
                    goSearchList(searchHistory[index]);
                  },
                  child: new Container(
                    margin: new EdgeInsets.only(
                        left: ScreenUtil.getInstance().setWidth(36)),
                    width: ScreenUtil.screenWidthDp -
                        ScreenUtil.getInstance().setWidth(85 + 36 + 36 + 136),
                    child: new Text(
                      searchHistory[index],
                      style: new TextStyle(
                        fontSize: ScreenUtil.getInstance().setWidth(42),
                      ),
                      maxLines: 1,
                    ),
                  ),
                ),
                new GestureDetector(
                  child: new Container(
                    color: Colors.white,
                    width: ScreenUtil.getInstance().setWidth(100),
                    height: ScreenUtil.getInstance().setWidth(100),
                    padding: new EdgeInsets.all(
                      ScreenUtil.getInstance().setWidth(30),
                    ),
                    child: new Image.asset(
                      "images/search_delete.png",
                      width: ScreenUtil.getInstance().setWidth(40),
                      height: ScreenUtil.getInstance().setWidth(40),
                      fit: BoxFit.fill,
                    ),
                  ),
                  onTap: () {
                    searchHistory.removeAt(index);
                    _changeHistory(searchHistory);
                    setState(() {});
                  },
                ),
              ],
            ),
          ),
          new Container(
            height: ScreenUtil.getInstance().setWidth(2),
            width: ScreenUtil.screenWidthDp,
            color: UGrowColors.getColor("#eeeeee"),
          ),
        ],
      ),
    );
  }

  Future<bool> _changeHistory(List<String> historyStr) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    print("_changeHistory:" + historyStr.toString());
    return sharedPreferences.setStringList("historyList", historyStr);
  }

  Future<List<String>> _getHistory() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    print("_getHistory");
    return sharedPreferences.getStringList("historyList");
  }

  void goSearchList(String searchKey) {
    Navigator.of(context).push(
      new CupertinoPageRoute(
        builder: (context) {
          return new ScreenSearchResult(keyWord: searchKey);
        },
      ),
    );
  }
}
